package mediator;

import java.util.Random;

import javax.swing.SwingWorker;

/**
 * Class used to simulate the actions of another user
 * @author Echipa Racheta
 *
 */
public class Simulator extends SwingWorker<Integer, Integer> {
	  private static int DELAY;
	  Mediator med;
	  
	  public Simulator(Mediator m) {
		  med = m;
	  }

	  @Override
	  protected Integer doInBackground() throws Exception {
		DELAY = 10000;
		try {
			Thread.sleep(3000);
			// The Buyer has Logged in
			med.login();
			System.out.println("The Buyer has logged in.");
			Thread.sleep(3000);
			
			if (med.cr.type.equals("Buyer"))
			{
				
				
				// Launch Offer
				System.out.println("The Buyer has launched an offer.");
				med.selectedRow = 0;
				med.clickPopupItemLaunchOffer();
				Thread.sleep(4000);
				
				// The Seller has made an offer
				System.out.println("The Seller has made an offer");
				med.setStatus("service1", "Seller1", "Offer Made");
				Thread.sleep(5000);
				
				// The Seller has made an offer
				System.out.println("The Seller2 has made an offer");
				med.setStatus("service1", "Seller2", "Offer Made");
				Thread.sleep(5000);
				
				// The Buyer launches another Offer
				System.out.println("The Buyer has launched another offer.");
				med.selectedRow = 5;
				med.clickPopupItemLaunchOffer();
				Thread.sleep(4000);
	
				// The Buyers accepts 1st offer
				System.out.println("The Buyers accepts 1st offer");
				med.selectedRow = 1;
				med.clickPopupItemAcceptOffer();
				Thread.sleep(5000);
				
				// The transfer has started
				System.out.println("The transfer has started");
				
				
				// The Seller has made an offer
				System.out.println("The Seller has made an offer");
				med.setStatus("service3", "Seller3", "Offer Made");
				Thread.sleep(5000);
	
				// The Buyers accepts offer
				System.out.println("The Buyers accepts offer");
				med.selectedRow = 8;
				med.clickPopupItemAcceptOffer();
				Thread.sleep(5000);
				
				// The Buyer has logged out
				System.out.println("The Buyer has logged out");
				med.logout();
			}
			else // Seller
			{
//				Thread.sleep(3000);
//				// The Seller has Logged in
//				med.login();
//				System.out.println("The Seller has logged in.");
//				Thread.sleep(3000);
				
				// The Seller has made an offer
				System.out.println("The Seller has made an offer");
				med.selectedRow = 1;
				med.clickPopupItemMakeOffer();
				Thread.sleep(5000);
				
				// The Seller has made an offer
				System.out.println("The Seller has made an offer");
				med.selectedRow = 2;
				med.clickPopupItemMakeOffer();
				Thread.sleep(5000);
				
				// An offer was accepted:
				System.out.println("An offer was accepted");
				med.setStatus("service0", "Buyer3", "Offer Accepted");
				med.transfer(1);
				Thread.sleep(5000);
				
				// The Seller has made an offer
				System.out.println("The Seller has made an offer");
				med.selectedRow = 5;
				med.clickPopupItemMakeOffer();
				Thread.sleep(5000);
				
				// The Seller has made an offer
				System.out.println("The Seller has made an offer");
				med.selectedRow = 6;
				med.clickPopupItemMakeOffer();
				Thread.sleep(5000);
				
				// One offer was exceeded
				System.out.println("One offer was exceeded");
				med.setStatus("service1", "Buyer3", "Offer Exceeded");
				Thread.sleep(5000);
				
				// The Seller has made an offer
				System.out.println("The Seller has drop the exceeded offer");
				med.selectedRow = 5;
				med.clickPopupItemDropAuction();
				Thread.sleep(5000);
				
				// An offer was accepted:
				System.out.println("An offer was accepted");
				med.setStatus("service0", "Buyer2", "Offer Accepted");
				med.transfer(2);
				Thread.sleep(5000);
				
				// The transfer was failed
				System.out.println("The transfer was failed");
				med.network.transferFailed(2);
				Thread.sleep(5000);
				
				// The Seller has made an offer
				System.out.println("The Seller has drop the exceeded offer");
				med.selectedRow = 6;
				med.clickPopupItemDropAuction();
				Thread.sleep(5000);
				
				// The Seller has logged out
				System.out.println("The Seller has logged out");
				med.logout();
			
			}

			
		}
		catch (InterruptedException e) {
		    e.printStackTrace();
		}
		
		Random r = new Random();
		
		
		int random = r.nextInt(20);
		
		
		
		return 0;
	  }
	   

	  @Override
	  protected void done() {
	    if (isCancelled()) {
	    	// TODO: CANCELED TASK
	    }
	    else {
	    	// TODO: TASK IS DONE
	    }
	  }	
}
